package cn.HL.pet.service.impl;

import cn.HL.pet.domain.PetType;
import cn.HL.pet.mapper.PetTypeMapper;
import cn.HL.pet.service.IPetTypeService;
import cn.HL.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HL
 * @since 2022-06-25
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> findparent() {
        List<PetType> petType = petTypeMapper.loadAll();
        Map<Long,PetType> allPetType= new HashMap<>();
        List<PetType> firstPetType= new ArrayList<>();
        for (PetType department : petType) {
            allPetType.put(department.getId(),department);
        }
        for (PetType tmp : petType) {
            if(tmp.getPid()==null){
                firstPetType.add(tmp);
            }else{
                PetType parent = allPetType.get(tmp.getPid());
                if(parent!=null){
                    parent.getChildren().add(tmp);
                }

            }
        }
        return firstPetType;
    }
}
